package user

import (
	"context"
	"strings"

	"mall/service/recommended/api/internal/svc"
	"mall/service/recommended/api/internal/types"
	client "mall/service/recommended/common/gorseclient"
	//"github.com/zhenghaoz/gorse/client"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateUserLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserLogic {
	return &CreateUserLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateUserLogic) CreateUser(req *types.UserInfo) (resp *types.RowAffectedResp, err error) {
	if len(strings.TrimSpace(req.UserId)) == 0 {
		return &types.RowAffectedResp{}, err
	}

	var user = client.User{
		UserId: req.UserId,
	}
	if len(strings.TrimSpace(req.Labels)) > 0 {
		user.Labels = strings.Split(req.Labels, ",")
	}
	if len(strings.TrimSpace(req.Subscribe)) > 0 {
		user.Subscribe = strings.Split(req.Subscribe, ",")
	}
	if len(strings.TrimSpace(req.Comment)) > 0 {
		user.Comment = req.Comment
	}

	res, err := l.svcCtx.GorseClient.InsertUser(l.ctx, user)
	if err != nil {
		logx.Errorf("CreateUser err:%+v", err)
		return nil, err
	}

	return &types.RowAffectedResp{
		RowAffected: uint32(res.RowAffected),
	}, nil
}
